package com.opener.common.code.assistant.gen;

import com.opener.common.code.assistant.model.Tab;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

public class AssistantUtils {

    private static String PROJECT_DIR = null;
    private static String BASE_PKG = null;
    private static String MYBATIS_MAPPER_PATH = null;
    

    private static String srcPath = null;
    private static Map<String, String> classSuffixConf = new HashMap();
    private static Map<String, String> pkgSuffixConf = new HashMap();
    private static Map<String, Class> generatorConf = new HashMap();
    static {
    }
    public static void setup(String projectDir, String basePkg, String mapperDir){
        PROJECT_DIR = projectDir;
        BASE_PKG = basePkg;
        MYBATIS_MAPPER_PATH = PROJECT_DIR+"/src/main/resources/"+mapperDir+"/mappers";
        srcPath = PROJECT_DIR +"/src/main/java";

        classSuffixConf.put("model","");
        classSuffixConf.put("search","SearchArgs");
        classSuffixConf.put("dao","Dao");
        classSuffixConf.put("service","Service");
        classSuffixConf.put("service.impl","ServiceImpl");
        classSuffixConf.put("dao.xml","Dao");
        classSuffixConf.put("controller","Controller");

        pkgSuffixConf.put("model", BASE_PKG +".model");
        pkgSuffixConf.put("search", BASE_PKG +".model");
        pkgSuffixConf.put("dao", BASE_PKG +".dao");
        pkgSuffixConf.put("service", BASE_PKG +".service");
        pkgSuffixConf.put("service.impl", BASE_PKG +".service");
        pkgSuffixConf.put("dao.xml","");
        pkgSuffixConf.put("controller", BASE_PKG +".controller");

        generatorConf.put("model",ModelGenerator.class);
        generatorConf.put("search",SearchArgsGenerator.class);
        generatorConf.put("dao", DaoGenerator.class);
        generatorConf.put("service", ServiceGenerator.class);
        generatorConf.put("service.impl", ServiceImplGenerator.class);
        generatorConf.put("dao.xml", DaoMapperGenerator.class);
        generatorConf.put("controller", ControllerGenerator.class);
    }
    public static void gen(String mod, Tab tab,boolean createFile) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {

        Ctx  ctx = new Ctx();
        for (String k : classSuffixConf.keySet()){
            String pkg = pkgSuffixConf.get(k);
            if (mod !=null && mod.length()>0){
                pkg = pkg + "." + mod;
            }
            if ("service.impl".equalsIgnoreCase(k)){
                pkg = pkg +".impl";
            }
            String classSuffix = classSuffixConf.get(k);
            Class<Generator> generatorClass = generatorConf.get(k);

            String className = tab.getClassName()+classSuffix;
            String fp = String.format("%s/%s/%s.java", srcPath, pkg.replaceAll("\\.","/"), className );
            if ("dao.xml".equalsIgnoreCase(k)){
                fp = MYBATIS_MAPPER_PATH ;
                if (mod !=null && mod.length()>0){
                    fp = fp +"/"+ mod;
                }
                fp = fp+"/"+className+".xml";
            }
            System.out.println("fp:"+fp+"|pkg:"+pkg);
            File classFile = new File(fp);
            Ctx.MvcClass cls = new Ctx.MvcClass(pkg, tab, className, classFile);
            Generator generator = generatorClass.getConstructor(Ctx.class, Ctx.MvcClass.class).newInstance(new Object[]{ctx, cls});
            cls.setGenerator(generator);
            ctx.addMvcClass(k, cls);
        }
        Map<String, Ctx.MvcClass> classes = ctx.getMvcClasses();
        for (Ctx.MvcClass cls : classes.values()){
            Generator generator = cls.getGenerator();
            generator.gen(createFile);
        }


    }
}
